package com.lza.phone.service.impl;

import com.lza.Exception.CustomException;
import com.lza.phone.entity.*;
import com.lza.phone.entity.connectEntity.PhoneList;
import com.lza.phone.mapper.PhoneMapper;
import com.lza.phone.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lza.redis.RedisUtil;
import com.lza.response.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.concurrent.TimeUnit;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 李钊澳
 * @since 2021-08-28
 */
@Service
public class PhoneServiceImpl extends ServiceImpl<PhoneMapper, Phone> implements PhoneService {
    @Autowired
    BatteryService batteryService;
    @Autowired
    CameraService cameraService;
   /* @Autowired
    CapacityService capacityService;*/
    @Autowired
    PerformanceService performanceService;
    @Autowired
    ScreenService screenService;
    @Autowired
    SizeService sizeService;
    @Autowired
    SocService socService;
    @Autowired
   private RedisUtil redisUtil;

    @Override
    public PhoneList getOnePhone(Integer id) {
        return baseMapper.getOnePhone(id);
    }
//用redis搞一波
    @Override
    public List<PhoneList> getAllPhone() {
        //先在redis里面找是否有，有的话直接返回
       List<PhoneList> list = (List<PhoneList>)redisUtil.get("allPhone");
        //设置过期时间TimeUnit.DAYS          //天
        //     * TimeUnit.HOURS         //小时
        //     * TimeUnit.MINUTES       //分钟
        //     * TimeUnit.SECONDS       //秒
        //     * TimeUnit.MILLISECONDS  //毫秒
        //     * TimeUnit.NANOSECONDS   //毫微秒
        //     * TimeUnit.MICROSECONDS  //微秒
        //redisUtil.expire("allPhone", 30, TimeUnit.SECONDS);//设置30秒后过期
       if(CollectionUtils.isEmpty(list)){//如果为空，数据库拿
           List<PhoneList> allPhone = baseMapper.getAllPhone();
           redisUtil.set("allPhone",allPhone);
           return allPhone;
       }
        return list;
    }

    @Override
    public List<PhoneList> getAllPhoneByCpuMark() {
        return baseMapper.getAllPhoneByCpuMark();
    }

    @Override
    public void saveOnePhone(Phone phone) {
       // BeanUtils.copyProperties();可以复制过去
        int insert = baseMapper.insert(phone);
        if(insert==0){
            throw new CustomException(20001, "添加手机失败");
        }
        System.out.println(phone.getId());
        Camera camera=new Camera();
        camera.setCameraId(phone.getId());
        System.out.println("-------"+camera.getCameraId());
        cameraService.save(camera);
        Battery battery=new Battery();
        battery.setBatteryId(phone.getId());
        batteryService.save(battery);
        Performance performance=new Performance();
        performance.setPerformanceId(phone.getId());
        performanceService.save(performance);
        Screen screen=new Screen();
        screen.setScreenId(phone.getId());
        screenService.save(screen);
        Size size=new Size();
        size.setSizeId(phone.getId());
        sizeService.save(size);
        Soc soc=new Soc();
       soc.setSocId(phone.getId());
        socService.save(soc);
    }
    //另外一个方法
    @Override
    public Result allPhone() {
        List<PhoneList> phoneLists = baseMapper.AllPhone();
        System.out.println("------"+phoneLists);
        return Result.ok().getData("第二种方法查找手机成功", phoneLists);
    }


}
